﻿using System;
using System.Collections.Generic;
using System.IO;

namespace WebCamDeskLib
{
    public class WebCamList
    {
        /// <summary>
        /// List of WebCams
        /// </summary>
        public List<WebCam> WebCams;
        /// <summary>
        /// Counter for looping through all WebCams in <see cref="WebCamList"/>
        /// </summary>
        private int CamLoopCounter = -1;
        /// <summary>
        /// Initializes a new instance of the <see cref="WebCamList"/> class.
        /// </summary>
        public WebCamList()
        {
            WebCams = new List<WebCam>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="WebCamList"/> class.
        /// </summary>
        /// <param name="aFileName">Name of a file.</param>
        public WebCamList(string aFileName)
        {
            WebCams = new List<WebCam>();
            LoadList(aFileName);
        }

        /// <summary>
        /// Gets the random web cam.
        /// </summary>
        public WebCam GetRandomWebCam()
        {
            Random myRandom = new Random();
            return WebCams[myRandom.Next(WebCams.Count - 1)];
        }

        /// <summary>
        /// Gets the next web cam.
        /// </summary>
        public WebCam GetNextWebCam()
        {
            CamLoopCounter++;
            if (CamLoopCounter > WebCams.Count)
                CamLoopCounter = 0;
            return WebCams[CamLoopCounter];
        }
        /// <summary>
        /// Gets the WebCam for desc.
        /// </summary>
        /// <param name="aDesc">A desc.</param>
        /// <returns></returns>
        public WebCam GetWebCamForDesc(string aDesc)
        {
            foreach (WebCam item in this.WebCams)
            {
                if (item.Description == aDesc)
                {
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// Loads the WebCamlist.
        /// </summary>
        /// <param name="aFileName">Name of a file.</param>
        public void LoadList(string aFileName)
        {
            using (StreamReader myReader = new StreamReader(aFileName))
            {
                string line;
                // Read and display the lines from the file until the end 
                // of the file is reached.
                while ((line = myReader.ReadLine()) != null)
                    WebCams.Add(CreateWebcam(line));
            }
        }

        public void SaveList(string aFileName)
        {
            using (StreamWriter myWriter = new StreamWriter(aFileName))
            {
                foreach (WebCam myCam in WebCams)
                {
                    myWriter.WriteLine(myCam.ToLine());
                }
            }
        }
               
        /// <summary>
        /// Creates a Webcam from a Definitionline in Ini-File
        /// </summary>
        /// <param name="aWebcamDef">A webcam def.</param>
        /// <returns></returns>
        private WebCam CreateWebcam(string aWebcamDef)
        {
            WebCam myWebCam = new WebCam();
            string aLine = aWebcamDef.Replace('"', ' ');
            string[] myParams = aLine.Split(',');

            myWebCam.Active = (myParams[0] == "1");
            myWebCam.Description = myParams[0].Trim();
            myWebCam.Adress = myParams[1].Trim();
            myWebCam.Refresh = Convert.ToInt16(myParams[3]);
            myWebCam.PicSize = myParams[4].Trim();
            myWebCam.SortOrder = Convert.ToInt16(myParams[5]);
            myWebCam.Active = (myParams[6].Trim() == "True");
            myWebCam.Rating = Convert.ToInt16(myParams[7]);
            myWebCam.Land = myParams[8].Trim();
            myWebCam.Ort = myParams[9].Trim();

            if (!myWebCam.Adress.Contains(@"http://"))
                myWebCam.Adress = @"http://" + myWebCam.Adress;
            return myWebCam;
        }

        public WebCam AddWebCam()
        {
            WebCam myCam = new WebCam();
            myCam.Description = "Neue Webcam";
            WebCams.Add(myCam);
            return myCam;
        }
    }
}